﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WXYW.BLL;

namespace WXYW
{
    public partial class AddProvider : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindProductTypeList();
            }
        }

        private void BindProductTypeList()
        {
            //ApplicationProductService productTypeService = new ApplicationProductService();
            ProductClassService classService = new ProductClassService();
            productTypeDropdownList.DataSource = classService.GetAllClass();
            productTypeDropdownList.DataTextField = "Name";
            productTypeDropdownList.DataValueField = "ID";
            productTypeDropdownList.DataBind();
        }

        protected void SaveButton_Click(object sender, EventArgs e)
        {
            try
            {
                ProviderService providerService = new ProviderService();
                providerService.AddProvider(codeTxtBox.Text, nameTxtBox.Text, Convert.ToInt32(productTypeDropdownList.SelectedValue), relationTxtBox.Text,
                    contact1TxtBox.Text, phone1TxtBox.Text, mobilePhone1TxtBox.Text, fax1TxtBox.Text, email1TxtBox.Text, contact2TxtBox.Text,
                    phone2TxtBox.Text, mobilePhone2TxtBox.Text, fax2TxtBox.Text, email2TxtBox.Text, postcodeTxtBox.Text, cityTxtBox.Text, addressTxtBox.Text,
                    ContextService.CurrentAccount);
                messageLbl.Text = "添加成功，请继续添加下一个。";
                ClearForms();
            }
            catch (Exception ex)
            {
                messageLbl.Text =  string.Format("系统错误:{0}", ex.Message);
            }
        }

        private void ClearForms()
        {
            codeTxtBox.Text = string.Empty;
            nameTxtBox.Text = string.Empty;
            relationTxtBox.Text = string.Empty;
            contact1TxtBox.Text = string.Empty;
            contact2TxtBox.Text = string.Empty;
            phone1TxtBox.Text = string.Empty;
            phone2TxtBox.Text = string.Empty;
            mobilePhone1TxtBox.Text = string.Empty;
            mobilePhone2TxtBox.Text = string.Empty;
            fax1TxtBox.Text = string.Empty;
            fax2TxtBox.Text = string.Empty;
            email1TxtBox.Text = string.Empty;
            email2TxtBox.Text = string.Empty;
            postcodeTxtBox.Text = string.Empty;
            cityTxtBox.Text = string.Empty;
            addressTxtBox.Text = string.Empty;            
        }
    }
}
